//
//  PPHomeRootViewController.m
//  pickpic
//
//  Created by 胡文广 on 2017/3/20.
//  Copyright © 2017年 MicroDream. All rights reserved.
//

#import "PPHomeRootViewController.h"
#import "PPHomeViewController.h"
#import "PPAbstractNavigationController.h"
#import "PPDisCoverViewController.h"
@interface PPHomeRootViewController ()
@property (nonatomic,strong) PPHomeViewController *homeVc;
@property (nonatomic,strong) PPDisCoverViewController *disCoverVc;
@property (nonatomic,strong) NSMutableArray *tabbarItems;
@end

@implementation PPHomeRootViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    for (UITabBarItem * i in self.tabBar.items) {
        i.titlePositionAdjustment = UIOffsetMake(0, -4);
        i.imageInsets = UIEdgeInsetsMake(-1, 0, 1, 0);
    }
    
}
- (NSMutableArray *)tabbarItems{

    if (!_tabbarItems) {
        _tabbarItems = [NSMutableArray array];
    }
    return _tabbarItems;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tabBar.barTintColor = WHITE;
    self.tabBar.tintColor = BLACK;
    
    // 初始化所有的子控制器
    [self setupAllChildViewControllers];

    self.selectedViewController = [self.viewControllers objectAtIndex:1];
}


/**
 *  初始化所有的子控制器
 */
- (void)setupAllChildViewControllers
{
    // 相册
    PPHomeViewController *home = [[PPHomeViewController alloc] init];
//    home.tabBarItem.badgeValue = @"N";
    [self setupChildViewController:home title:@"照片" imageName:@"tabbar_home" selectedImageName:@"tabbar_home_Sel"];
    self.homeVc = home;
    
    // 发现
    PPDisCoverViewController *disCover = [[PPDisCoverViewController alloc] init];
//    disCover.tabBarItem.badgeValue = @"8";
    [self setupChildViewController:disCover title:@"发现 " imageName:@"tabbar_discover" selectedImageName:@"tabbar_discover_Sel"];
    self.disCoverVc = disCover;
}

/**
 *  初始化一个子控制器
 *
 *  @param childVc           需要初始化的子控制器
 *  @param title             标题
 *  @param imageName         图标
 *  @param selectedImageName 选中的图标
 */
- (void)setupChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    // 1.设置控制器的属性
    childVc.title = title;
    // 设置图标
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    // 设置选中的图标
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    
    childVc.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    // 2.包装一个导航控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:childVc];
    [self addChildViewController:nav];
    
    [self.tabbarItems addObject:childVc.tabBarItem];

}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{

    PPLog(@"选中了%@",item.title);
}

@end
